package com.ruoyi.service.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.service.domain.ServiceOrders;
import com.ruoyi.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单信息表Controller
 * 
 * @author ZDL
 * @date 2024-11-13
 */
@RestController
@RequestMapping("/service/orders")
public class ServiceOrdersController extends BaseController
{
    @Autowired
    private IServiceOrdersService serviceOrdersService;
    @Autowired
    private IServiceAddressService serviceAddressService;
    @Autowired
    private IServicePackagesService servicePackagesService;
    @Autowired
    private IServiceSuppliesService serviceSuppliesService;
    @Autowired
    private IServiceComboService serviceComboService;
    @Autowired
    private IServiceItemsService serviceItemsService;
    @Autowired
    private IServiceItemscomboService serviceItemscomboService;
    @Autowired
    private IServiceNurseService serviceNurseService;
    @Autowired
    private IServiceOrdersitemsService serviceOrdersitemsService;
    @Autowired
    private IServicePackagescomboService servicePackagescomboService;
    @Autowired
    private IServicePackagessuppliesService servicePackagessuppliesService;
    @Autowired
    private IServicePayService servicePayService;
    @Autowired
    private IServicePictureService servicePictureService;


    /**
     * 查询订单信息表列表
     */
    @PreAuthorize("@ss.hasPermi('service:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServiceOrders serviceOrders)
    {
        startPage();
        List<ServiceOrders> list = serviceOrdersService.selectServiceOrdersList(serviceOrders);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('service:orders:listall')")
    @GetMapping("/listall")
    public TableDataInfo listAll(ServiceOrders serviceOrders)
    {
        startPage();
        List<ServiceOrders> list = serviceOrdersService.selectServiceOrdersListAll(serviceOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单信息表列表
     */
    @PreAuthorize("@ss.hasPermi('service:orders:export')")
    @Log(title = "订单信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceOrders serviceOrders)
    {
        List<ServiceOrders> list = serviceOrdersService.selectServiceOrdersList(serviceOrders);
        ExcelUtil<ServiceOrders> util = new ExcelUtil<ServiceOrders>(ServiceOrders.class);
        util.exportExcel(response, list, "订单信息表数据");
    }

    /**
     * 获取订单信息表详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:orders:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(serviceOrdersService.selectServiceOrdersByOrderId(orderId));
    }

    /**
     * 新增订单信息表
     */
    @PreAuthorize("@ss.hasPermi('service:orders:add')")
    @Log(title = "订单信息表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceOrders serviceOrders)
    {
        return toAjax(serviceOrdersService.insertServiceOrders(serviceOrders));
    }

    /**
     * 修改订单信息表
     */
    @PreAuthorize("@ss.hasPermi('service:orders:edit')")
    @Log(title = "订单信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceOrders serviceOrders)
    {
        return toAjax(serviceOrdersService.updateServiceOrders(serviceOrders));
    }

    /**
     * 删除订单信息表
     */
    @PreAuthorize("@ss.hasPermi('service:orders:remove')")
    @Log(title = "订单信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(serviceOrdersService.deleteServiceOrdersByOrderIds(orderIds));
    }

    private void checkStatus(Long orderId, Long userId, int statusNew) {
        ServiceOrders serviceOrdersOld = serviceOrdersService.selectServiceOrdersByOrderId(orderId);
        if (serviceOrdersOld.getOrderUserid() != userId) {
            throw new RuntimeException("用户状态异常");
        }
        if(serviceOrdersOld.getOrderServiceflag() == 4){
            throw new RuntimeException("订单已取消，暂无法操作");
        }
        switch (statusNew) {
            case 1:
                ///创建订单
                break;
            case 2:///取消订单
                if(serviceOrdersOld.getOrderServiceflag() > 8){
                    throw new RuntimeException("订单进入服务状态，暂无法取消");
                }
                break;
            case 3:
                ///不明白这个审核取消啥意思呀
                break;
            case 4:///订单进行取消
                if(serviceOrdersOld.getOrderServiceflag() != 2){
                    throw new RuntimeException("订单取消申请未提交或未通过，暂无法取消");
                }
                break;
            case 5:///上报
                if(serviceOrdersOld.getOrderServiceflag() > 8){
                    throw new RuntimeException("订单进入服务状态，无法上报");
                }
                break;
            case 6:///护士长分配
                if((serviceOrdersOld.getOrderServiceflag() != 1) && (!"0".equals(servicePayService.selectServicePayByPayId(serviceOrdersOld.getOrderPay()).getPayStatus()))){
                    throw new RuntimeException("订单未支付预付金额，无法进入分配");
                }
                break;
            case 7:///分配
                if((serviceOrdersOld.getOrderServiceflag() != 6) && (!"0".equals(servicePayService.selectServicePayByPayId(serviceOrdersOld.getOrderPay()).getPayStatus()))){
                    throw new RuntimeException("订单未支付预付金额或未分配");
                }
                break;
            case 8:///接受
                if((serviceOrdersOld.getOrderServiceflag() != 7)){
                    throw new RuntimeException("订单未分配");
                }
                break;
            case 9:///出发
                if((serviceOrdersOld.getOrderServiceflag() != 8)){
                    throw new RuntimeException("流程不合规范");
                }
                break;
            case 10:///到达进行服务
                if((serviceOrdersOld.getOrderServiceflag() != 9)){
                    throw new RuntimeException("流程不合规范");
                }
                break;
            case 11:///服务完成
                if((serviceOrdersOld.getOrderServiceflag() != 10)){
                    throw new RuntimeException("流程不合规范");
                }
                break;
            case 12:///订单完成
                if((serviceOrdersOld.getOrderServiceflag() != 11) && (!"0".equals(servicePayService.selectServicePayByPayId(serviceOrdersOld.getOrderPay()).getPayStatusfinish()))){
                    throw new RuntimeException("流程不合规范或订单未支付尾款");
                }
                break;
            case 13:///评价
                if((serviceOrdersOld.getOrderServiceflag() != 12)){
                    throw new RuntimeException("流程不合规范");
                }
                break;
            case 14:
                ///重新分配，暂未开发

        }

    }
}
